<?php

namespace app\controllers\admin;
use app\models\Admin as Model;
use app\models\user\User;
use app\models\user\UserLevel;
use app\redis\Cache;
use app\util\JWT;
use Yii;

/**
 * Class AdminController
 * @package app\controllers\admin
 */
class AdminController extends \app\controllers\admin\base\AdminController
{
    /**
     * Administrator login
     *
     * @return \yii\web\Response
     */
    public function actionLogin()
    {
        $username = $this->post('username');
        $password = $this->post('password');

        if (!$username || !$password) {
            return $this->fail('请输入正确的手机号码或者密码');
        }

        // var_dump($username, $password);exit;


        // php -r "echo md5('123456'. 'admin-salt');"

        // 去掉加密
        // $password = md5($password . Yii::$app->params['ADMIN_SALT']);

        $user = User::findOne(['mobile' => $username]);
        if (!$user) {
            return $this->fail('手机号码错误');
        }

        // CEO 才可以看
        // if ($user->level < UserLevel::$keys['vip1']) {
        if ($user->level < UserLevel::$keys['vip4']) {
            return $this->fail('等级不足');
        }

        // 如果存在密码
        if ($user->password) {
            if ($user->password != $password) {
                return $this->fail('密码错误');
            }
        }


        $token = JWT::createToken(
            // [
            //     [ 'username' => $user->nickname ],
            //     [ 'id' => $user->id ],
            //     [ 'mobile' => $username ],
            //     // [ 'password' => $user->password ],
            // ],
            // [ 'username' => $username ],
            [ 'id' => $user->id ],
            Yii::$app->params['JWT_ADMIN_KEY'],
            Yii::$app->params['JWT_ADMIN_EXPIRED']
        );

        return $this->success(['token' => $token], '登录成功');

        return $this->fail('登录失败,请检查用户名或者密码是否正确');
    }

    /**
     * Get administrator info
     *
     * @return \yii\web\Response
     */
    public function actionGetInfo()
    {
        // var_dump($this->user);exit;

        // $user = Model::getInfo($this->user->username);
        $user = Model::getInfo($this->user->id);

        if ($user !== false) {
            return $this->success($user);
        }

        return $this->fail('获取用户信息失败');
    }

    // 是否存在密码
    public function actionHasPassword()
    {
        $id = $this->user->id;

        $user = User::findOne(['id' => $id]);
        if ($user) {
            return $this->success([
                'has' => $user->password != '',
            ]);
        }

        return $this->fail();
    }

    public function actionPassword()
    {
        $password_old = $this->post('password_old');
        $password = $this->post('password');
        $password2 = $this->post('password2');

        if ($password != $password2) {
            return $this->fail('两次密码不一致');
        }

        if (strlen($password) < 6) {
            return $this->fail('密码长度不能小于6位');
        }

        // $password_old = md5($password_old . Yii::$app->params['ADMIN_SALT']);
        // $model = Model::findOne(['username' => $this->user->username, 'password' => $password_old]);
        // if ($model) {
        //     // $password = md5($password . Yii::$app->params['ADMIN_SALT']);
        //     $model->password = $password;
        //     if ($model->save()) {
        //         return $this->success();
        //     }
        // } else {
        //     return $this->fail('旧密码错误');
        // }

        $id = $this->user->id;
        $user = User::findOne(['id' => $id]);
        if ($user) {

            if ($user->password) {
                if ($user->password != $password_old) {
                    return $this->fail('旧密码错误');
                }
            }

            $user->password = $password;
            $user->save();

            return $this->success();

        }

        return $this->fail();
    }

    /**
     * Flush all redis
     *
     * @return \yii\web\Response
     */
    public function actionClear()
    {
        if (Cache::flushAll()) {
            return $this->success();
        }

        return $this->fail();
    }
}